package com.nidu.demo.notice.executor.command;

import com.alibaba.cola.dto.SingleResponse;
import com.nidu.demo.notice.convertor.NoticeConvertor;
import com.nidu.demo.notice.dto.NoticeAddCmd;
import com.nidu.demo.notice.dto.NoticeCO;
import com.nidu.demo.notice.gateway.NoticeGateway;
import com.nidu.demo.notice.model.Notice;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * 通知公告添加命令执行器
 *
 * @author auto
 */
@Component
@RequiredArgsConstructor
public class NoticeAddCmdExe {

    private final NoticeConvertor convertor;
    private final NoticeGateway gateway;

    @Transactional
    public SingleResponse<NoticeCO> execute(NoticeAddCmd cmd) {
        // 1. 转换为领域对象
        Notice notice = Notice.create(cmd.getTitle(), cmd.getContent(), cmd.getType());

        // 2. 保存通知公告
        Notice saved = gateway.create(notice);

        // 3. 转换为DTO返回
        NoticeCO noticeCO = convertor.toClientObject(saved);
        return SingleResponse.of(noticeCO);
    }

}
